import numpy as np
import pandas as pd
import os
import glob
import re
import warnings
from program.pack.append_df_to_excel import append_df_to_excel
from pandas import to_datetime
from functools import reduce

warnings.filterwarnings("ignore")

# 列显示不全，进行设置
pd.set_option('display.max_columns', 500)
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
pd.set_option('display.width', 180)  # 设置打印宽度(**重要**)



def func_0x(path_inputs=[r'C:\Users\wangshuan\Desktop\21.12.08\tai40-1'], endswith_false='Data.xlsx', endswith='.xlsx'):
    k = 0
    dfs_1 = pd.DataFrame()
    for path_input in path_inputs:  # 支持多文件夹遍历
        # os.walk(file_path) 深度遍历file_path下的所有子文件夹及文件
        for root_dirs, sub_dirs, files in os.walk(path_input):
            # 读取xlsx,以key_id为键
            for file in files:
                # 进行条件筛选   以非~$'开头(解决读取文档已打开问题)；选择以file_name_0结尾的文档
                if (file.startswith('~$') is False) and (file.endswith(endswith)) and (
                        file.lower().endswith('data.xlsx') is False):
                    # 构造文件的绝对路径 = 文件夹路径 + 文件名
                    try:
                        file_path = os.path.join(root_dirs, file)
                        data_1 = pd.read_csv(file_path,'gbk')
                        data_1['文件名'] = str(file)
                        # print(data_1.values[0][1],data_1.values[0][2],data_1.values[0][4],data_1.values[0][5],data_1.values[0][6])
                        dfs_1 = pd.concat([dfs_1, data_1])
                        k += 1
                        print(file, '第', k, '个表读取成功')
                    except Exception as e:
                        print(file, e)
    # dfs_1.sort_values(by=['文件名'], ascending=True, inplace=True)

    return dfs_1


def main_data(endswith=".xlsx", endswith_false="data.xlsx"):
    dfs_0 = func_0x(path_inputs, endswith=endswith, endswith_false=endswith_false)
    append_df_to_excel(path_out, dfs_0, startcol=0, startrow=0,
                       index=False)
    print('------------返回data文件程序已结束-----------')
    return dfs_0


# --------------data--------------

if __name__ == "__main__":
    # 数据源地址
    path_inputs = [r'W:\wangshuan\Tai-30\zonce\out0105']
    # 数据清洗输出位置
    path_out = r'W:\wangshuan\Tai-30\zonce\out0105.xlsx'
    #  注意对表文件进行筛选 以 endswith=".xlsx"， 不以  endswith_false="data.xlsx"结束的文件
    data_data = main_data(endswith=".csv", endswith_false="data.xlsx")
